/**
 * Created by Xiaoming on 2016/4/14.
 */
function scro(pId) {
    var top = $(document).scrollTop();
    var menu = $(pId).find("a");
    if (top == 0) {
        menu.each(function (index) {
            if (index == 0) {
                $(this).addClass("active");
            }
        });
    }
}

/*首页滚动监听*/
function scroll(pId, cId, domClass) {
    $(window).scroll(function () {
        var items = $(cId).find(domClass);
        var menu = $(pId);
        var top = $(document).scrollTop();
        var currentId = ""; //滚动条现在所在位置的item id
        items.each(function () {
            var m = $(this);
            //注意：m.offset().top代表每一个item的顶部位置
            if (top > m.offset().top - 150) {
                currentId = m.attr("id");
            } else {
                return false;
            }
        });
        var currentLink = menu.find(".active");
        if (currentId && currentLink.attr("class") != currentId) {
            currentLink.removeClass("active");
            menu.find("[class=" + currentId + "]").addClass("active");
        }
    });
}

/*首页滚动监听*/
function click_scroll(hash) {
    /*var top = $(document).scrollTop();
     if(top>=100){*/
    var $target = $(hash);
    $target = $target.length && $target || $('[name=' + hash.slice(1) + ']');
    if ($target.length) {
        var targetOffset = $target.offset().top - 40;
        $('html,body').animate({
            scrollTop: targetOffset
        }, 500);
        return false;
    }
}
//页面分左右两边用transform控制滚动
var transformScrolls = function (leftId, rightId, shop) {
    this.leftId = leftId;
    this.rightId = rightId;
    this.shop = shop;
    this.init();
    this.bind();
};
transformScrolls.prototype = {
    init: function () {
        var self = this;
        this.hei = $(window).height() - 40;//读取屏幕高度除去顶部栏高度
        this.scrollLeftHeight = 0;
        this.lastClick = '';
        if (this.shop) {
            this.hei = this.hei - 50;
            $("#goods_info").css({"height": (this.hei) + "px"});
            $("#goods_list").css({"height": (this.hei) + "px"});//设置父类框高度
            this.leftLi = $("#" + this.leftId).find(".center_left");
            $(".goods_center").css({"height": this.hei + "px"})
        } else {
            $("#left").css({"height": (this.hei) + "px"});
            $("#right").css({"height": (this.hei) + "px"});//设置父类框高度
            this.leftLi = $("#" + this.leftId).find(".center_left");
            $(".menu_center").css({"height": this.hei + "px"});
        }//判断是否是具体商店页面，如果是具体商店页面要减去底部悬浮购物车的高度
        this.scrollLeftHeight = $("#" + this.leftId).height() - this.hei;
        this.leftTouchObj = {
            startY: 0,
            scrollY: 0
        };
        this.rightTouchObj = {
            startY: 0,
            scrollY: 0
        };//初始化开始的位置和单位时间滚动距离
        for (var i = 0; i < this.leftLi.length; i++) {
            this.leftLi[i].onclick = function () {
                var id = this.id;
                if (self.lastClick != id) {
                    $("#" + self.rightId).css({"transform": "translateY(0px)", "-webkit-transform": "transition(0px)"})
                    self.lastClick = id;
                }
            }
        }
    },
    bind: function () {
        var self = this;
        $("#" + self.leftId).on("touchstart", function () {
            self.leftTouchObj.startY = event.touches[0].clientY;  // 纵坐标
            self.leftTouchObj.scrollY = 0;
        })
        $("#" + self.leftId).on("touchmove", function () {
            //可滚动区域大于零执行滚动函数
            if (self.scrollLeftHeight > 0) {
                event.preventDefault();
                var touches = event.touches[0];
                var endY = touches.clientY - self.leftTouchObj.startY;
                var endTranY = self.getY(self.leftId);//获取上次的位置
                var allEndY = 0;
                var allEndY = endY + endTranY;
                if (allEndY > 150) {
                    allEndY = 150
                }
                else {
                    if (allEndY < (self.scrollLeftHeight * -1 - 150)) {
                        allEndY = (self.scrollLeftHeight * -1 - 150);
                    }
                }
                ;
                $(this).css({
                    "transform": "translateY(" + allEndY + "px)",
                    "-webkit-transform": "transition(" + allEndY + "px)"
                });
                self.leftTouchObj.scrollY = touches.clientY - self.leftTouchObj.startY;
                self.leftTouchObj.startY = touches.clientY;//重置开始滚动的位置
            }
        });
        $("#" + this.leftId).on("touchend", function (e) {
            self.touchScroll(self.leftTouchObj.scrollY, self.leftId, self.scrollLeftHeight);
            //结束之后判断单位时间移动的距离如果距离大，设置一定的滚动速度
        });
        $("#" + self.rightId).on("touchstart", function () {
            self.rightTouchObj.startY = event.touches[0].clientY;  // 纵坐标
            self.rightTouchObj.scrollY = 0;
            self.scrollRightHeight = $("#" + self.rightId).height() - self.hei;
            //判断是否是具体商店页面，如果是具体商店页面要减去底部悬浮购物车的高度
        });
        $("#" + self.rightId).on("touchmove", function () {
            event.preventDefault();
            if (self.scrollRightHeight > 0) {
                var touches = event.touches[0];
                var endY = touches.clientY - self.rightTouchObj.startY;
                var endTranY = self.getY(self.rightId);
                var allEndY = 0;
                var allEndY = endY + endTranY;
                if (allEndY > 150) {
                    allEndY = 150
                }
                else {
                    if (allEndY < (self.scrollRightHeight * -1 - 150)) {
                        allEndY = (self.scrollRightHeight * -1 - 150);
                    }
                }
                ;
                $(this).css({
                    "transform": "translateY(" + allEndY + "px)",
                    "-webkit-transform": "transition(" + allEndY + "px)"
                });
                self.rightTouchObj.scrollY = touches.clientY - self.rightTouchObj.startY;
                self.rightTouchObj.startY = touches.clientY;
            }
        });
        $("#" + self.rightId).on("touchend", function (e) {
            self.touchScroll(self.rightTouchObj.scrollY, self.rightId, self.scrollRightHeight);
        });
        $(window).resize(function() {
            setTimeout(function () {
                self.init()
            }, 100)
        });
    },
    getY: function (Id) {
        var transform = $("#" + Id).css("transform");
        if (!transform) {
            transform = $("#" + Id).css("-webkit-transform");
        }
        if (transform == "none") {
            transform = 0
        }
        else {
            transform = transform.replace("matrix(", "").replace(")", "");
            transform = transform.split(',', 6);
            transform = transform[5];
            transform = parseInt(transform, 10)
        }
        if (!transform) {
            transform = 0
        }
        return transform
    },
    touchScroll: function (scrollY, Id, scrollHeight, g) {
        var self = this;
        var absScrollY = Math.abs(scrollY);
        var speed = 0;
        if (absScrollY >= 8) {
            speed = absScrollY
        }
        else {
            speed = 0
        };//根据单位时间位移的量来设置结束后的速度
        if (speed > 0) {
            if (scrollY < 0) {
                speed = speed * -1
            }
            //speed = Math.round(speed);
            speed = Math.round(speed) * 2;
            setTimeout(function () {
                self.touchScrollRun(speed, Id, scrollHeight);
            }, 2);
        }//根据单位时间位移的量来设置结束后的速度
        else {
            var translateY = 0;
            translateY = this.getY(Id);
            //不满足条件读取当前transform的值
            self.goToEnd(translateY, Id, scrollHeight);
        }
    },
    touchScrollRun: function (speed, Id, scrollHeight) {
        var self = this;
        if (Math.abs(speed) > 1) {
            var translateY = 0;
            if (speed > 0) {
                speed -= (speed * 0.05);
                translateY = this.getY(Id) + Math.round(speed);
            } else {
                speed -= (speed * 0.05);
                translateY = this.getY(Id) + Math.round(speed);
            }
            $("#" + Id).css("transform", "translateY(" + translateY + "px)");
            $("#" + Id).css("-webkit-transform", "translateY(" + translateY + "px)");
            if (translateY <= (scrollHeight * -1 - 30) || translateY >= 30) {
                this.goToEnd(translateY, Id, scrollHeight)
            }
            else {
                self.runScr = setTimeout(function () {
                    self.touchScrollRun(speed, Id, scrollHeight)
                }, 5)
            }
        }
        else {
            translateY = this.getY(Id) + speed;
            this.goToEnd(translateY, Id, scrollHeight);
        }
    },
    goToEnd: function (translateY, Id, scrollHeight) {
        var touchedCss = null;
        var endCss = null;
        if (scrollHeight > 0) {
            if (translateY < (scrollHeight * -1)) {
                touchedCss = {
                    transform: "translateY(" + (scrollHeight * -1) + "px)",
                    "-webkit-transform": "translateY(" + (scrollHeight * -1) + "px)",
                    "-webkit-transition": "0.2s ease 0s",
                    transition: "0.2s ease 0s"
                };
                endCss = {
                    transform: "translateY(" + (scrollHeight * -1) + "px)",
                    "-webkit-transform": "translateY(" + (scrollHeight * -1) + "px)",
                    "-webkit-transition": "", transition: ""
                }
            }
            else {
                if (translateY > 0) {
                    touchedCss = {
                        transform: "translateY(0px)",
                        "-webkit-transform": "translateY(0px)",
                        "-webkit-transition": "0.2s ease 0s",
                        transition: "0.2s ease 0s"
                    };
                    endCss = {
                        transform: "translateY(0px)",
                        "-webkit-transform": "translateY(0px)",
                        "-webkit-transition": "", transition: ""
                    }
                }
            }
        }
        if (translateY != 0) {
            if(endCss!=null&&touchedCss!=null){
                $("#" + Id).css(touchedCss);
                setTimeout(function () {
                    $("#" + Id).css(endCss)
                }, 200);
            }
            return true
        }
        else {
            $("#" + Id).css({transform: "translateY(0px)"});
            $("#" + Id).css({"-webkit-transform": "translateY(0px)"});
            return false
        }
    }
};
//下拉阻止事件冒泡
function stopPro(){
    var addressAbsoluteOrder = document.querySelector(".address_absolute_order");
    var paymentShow = document.getElementById("paymentShow");
    addressAbsoluteOrder.addEventListener('touchmove',moveStopPro,false);
    paymentShow.addEventListener('touchmove',moveStopPro,false);
    function moveStopPro(e){
        e.stopPropagation();
    }
}